//
//  HDLanguageSettingVC.m
//  HDSmartPenSDKDemo
//
//  Created by 牛的mini on 2016/12/26.
//  Copyright © 2016年 牛的mini. All rights reserved.
//

#import "HDLanguageSettingVC.h"

@interface HDLanguageSettingVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSArray *statusArray;
@property (weak, nonatomic) IBOutlet UISegmentedControl *languageSeg;
@property (weak, nonatomic) IBOutlet UIButton *uploadTimeBtn;

@property (nonatomic, assign) NSInteger segIndex;
@property (nonatomic, assign) NSInteger colorIndex;
@property (nonatomic, assign) NSInteger languageIndex;
@property (nonatomic, strong) HDBeaconManager *beaconManager;

@end

@implementation HDLanguageSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"参数设置";
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _myTableView.rowHeight = 50.0f;
    
    _dataArray = @[@"中文",@"英文",@"日文",@"韩文",@"法文"];
    _segIndex = 0;
    _beaconManager = [HDBeaconManager sharedBeacon];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(smPenIDNotification:) name:kBeacon_SMPen_ID_Notification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(smPenPowerNotification:) name:kBeacon_SMPen_Power_Notification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadStatusAction:)
                                                 name:kBeacon_SMPen_All_Info_Notification
                                               object:nil];
}

- (void)reloadStatusAction:(NSNotification *)noti
{
    NSArray *statusArray = noti.object;
    if (statusArray.count > 0)
    {
        _statusArray = statusArray;
        NSString *nowLanguage = statusArray[4];
        NSString *nowColor = statusArray[2];
        NSArray *languageArray = @[@"中文",@"英文",@"日语",@"韩语",@"法语"];
        NSArray *colorArray = @[@"红",@"绿",@"蓝",@"青",@"粉",@"黄",@"白"];
        for (NSInteger i=0;i<languageArray.count;i++)
        {
            NSString *language = languageArray[i];
            if ([language isEqualToString:nowLanguage])
            {
                _languageIndex = i;
                break;
            }
        }
        for (NSInteger i=0;i<colorArray.count;i++)
        {
            NSString *color = colorArray[i];
            if ([color isEqualToString:nowColor])
            {
                _colorIndex = i;
                break;
            }
        }
        [_myTableView reloadData];
    }
    [LCCoolHUD hideInView:self.view];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestSMPenInfoAction];
}

- (IBAction)segChangeAction:(UISegmentedControl *)sender {
    NSInteger selectIndex = sender.selectedSegmentIndex;
    if (selectIndex != _segIndex)
    {
        if (selectIndex == 0)
        {
            _dataArray = @[@"中文",@"英文",@"日语",@"韩语",@"法语"];
            //_uploadTimeBtn.hidden = NO;
        }
        else if (selectIndex == 1)
        {
            _dataArray = @[@"红",@"绿",@"蓝",@"青",@"粉",@"黄",@"白"];
            //_uploadTimeBtn.hidden = YES;
        }
        
        _segIndex = selectIndex;
        [_myTableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return  _dataArray.count; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger nowRow = indexPath.row;

    static NSString *identifeir = @"HDLanguageSettingVCCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifeir];
    cell.textLabel.text = _dataArray[nowRow];
    if ((_segIndex == 0 && _languageIndex == nowRow) || (_segIndex == 1 && _colorIndex == nowRow))
    {cell.imageView.image = [UIImage imageNamed:@"set_select"];}
    
    else
        cell.imageView.image = [UIImage imageNamed:@"set_normal"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSInteger nowRow = indexPath.row;
//    if (_segIndex == 0)
//    {
//        _languageIndex = nowRow;
//        _uploadTimeBtn.hidden = NO;
//        [_myTableView reloadData];
//    }
//    else if (_segIndex == 1)
//    {
//        _colorIndex = nowRow;
//        _uploadTimeBtn.hidden = YES;
//        [_myTableView reloadData];
//    }
}

- (IBAction)uploadTimeAction:(id)sender {
    [_beaconManager searchSMPenIDAction];
//    [_beaconManager readPowerAction];
}

- (void)smPenPowerNotification:(NSNotification *)noti
{
    NSString *powerStr = noti.object;
    NSString *tipStr = [NSString stringWithFormat:@"电量：%@%%", powerStr];
    [LCCoolHUD showSuccess:tipStr zoom:YES shadow:YES];
    NSLog(@"%@", tipStr);
}

- (void)smPenIDNotification:(NSNotification *)noti
{
    NSString *powerStr = noti.object;
    NSString *tipStr = [NSString stringWithFormat:@"电量：%@%%", powerStr];
    [LCCoolHUD showSuccess:tipStr zoom:YES shadow:YES];
    NSLog(@"%@", tipStr);
}

- (void)requestSMPenInfoAction {
    [[HDBeaconManager sharedBeacon] readAllInfoAction];
    [LCCoolHUD showLoading:@"正在获取" inView:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
